package com.capri.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.capri.R;
import com.capri.app.NurseHomeApp;
import com.capri.base.BaseActivity;
import com.capri.model.INurse;
import com.capri.model.bean.BaseResultBean;
import com.capri.model.impl.NurseModel;
import com.capri.utils.CommonSubscriber;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;
import rx.functions.Action0;

/**
 * 修改密码画面
 */
public class ModifyPasswordActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.edt_phone_num)
    EditText edtPhoneNum;
    @BindView(R.id.edt_password)
    EditText edtPassword;
    @BindView(R.id.edt_new_password_again)
    EditText edtNewPasswordAgain;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    private INurse model;

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_password;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
    }

    @Override
    public void setupData() {
        model = new NurseModel();
    }

    @OnClick(R.id.tv_confirm)
    public void onViewClicked(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_confirm:
                modifyPassword();
                break;
        }
    }

    private void modifyPassword() {
        if (TextUtils.isEmpty(edtPhoneNum.getText().toString())) {
            Toasty.error(ModifyPasswordActivity.this, "请输入旧密码", Toast.LENGTH_SHORT, true).show();
            return;
        }
        if (TextUtils.isEmpty(edtPassword.getText().toString())) {
            Toasty.error(ModifyPasswordActivity.this, "请输入新密码", Toast.LENGTH_SHORT, true).show();
            return;
        }
        if (TextUtils.isEmpty(edtNewPasswordAgain.getText().toString())) {
            Toasty.error(ModifyPasswordActivity.this, "请再次输入新密码", Toast.LENGTH_SHORT, true).show();
            return;
        }
        if (!edtPassword.getText().toString().equals(edtNewPasswordAgain.getText().toString())) {
            Toasty.error(ModifyPasswordActivity.this, "两次输入的新密码需要一致", Toast.LENGTH_SHORT, true).show();
            return;
        }
        model.modifyPwd(edtPhoneNum.getText().toString(), edtPassword.getText().toString(), edtNewPasswordAgain.getText().toString()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showDialog();
            }
        }).subscribe(new CommonSubscriber<BaseResultBean>() {
            @Override
            public void onSuccess(BaseResultBean response) {
                hideDialog();
                ModifyPasswordActivity.this.finish();
                NurseHomeApp.getInstance().finishActivity(ModifyPasswordActivity.this);
            }

            @Override
            public void onFailure(BaseResultBean response) {
                hideDialog();
                Toasty.error(ModifyPasswordActivity.this, response.getMsg(), Toast.LENGTH_SHORT, true).show();
            }
        });
    }
}
